package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.domain.Product;
import org.project.product.android.pfe.fanta.util.Utils;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class EanSaisir extends Activity {

	private static final int PAD_RESET = 10;
	private static final int PAD_DELETE = 11;

	private EditText eanInput;

	/** The handler that will receive loading messages. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		this.setContentView(R.layout.rechercheean);

		// Bind text field
		eanInput = (EditText) findViewById(R.id.entryEAN);

		// Bind pad buttons
		findViewById(R.id.num0).setOnClickListener(new PadListener(0));
		findViewById(R.id.num1).setOnClickListener(new PadListener(1));
		findViewById(R.id.num2).setOnClickListener(new PadListener(2));
		findViewById(R.id.num3).setOnClickListener(new PadListener(3));
		findViewById(R.id.num4).setOnClickListener(new PadListener(4));
		findViewById(R.id.num5).setOnClickListener(new PadListener(5));
		findViewById(R.id.num6).setOnClickListener(new PadListener(6));
		findViewById(R.id.num7).setOnClickListener(new PadListener(7));
		findViewById(R.id.num8).setOnClickListener(new PadListener(8));
		findViewById(R.id.num9).setOnClickListener(new PadListener(9));
		findViewById(R.id.numDel).setOnClickListener(
		        new PadListener(PAD_DELETE));
		findViewById(R.id.numReset).setOnClickListener(
		        new PadListener(PAD_RESET));

		// Set focus on text EAN text field and open keyboard
		eanInput.requestFocus();

	}

	public void searchProductByEan(View v) {
		if (Utils.checkEditText(getApplicationContext(), eanInput)) {

			String ean = eanInput.getText().toString();
			Product product = DataManagerFacade.getInstance(this)
			        .searchProductByEan(ean);
			if (product == null) {
				Toast.makeText(this, "No product matching this ean",
				        Toast.LENGTH_SHORT).show();
			} else {
				Utils.lancerActivity(this, ProductViewActivity.class, product);
			}
		}

	}

	private class PadListener implements OnClickListener {

		private final int button;

		public PadListener(int button) {
			this.button = button;
		}

		@Override
		public void onClick(View v) {
			switch (button) {
				case 0:
				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
				case 7:
				case 8:
				case 9:
					eanInput.append(Integer.toString(button));
					break;
				case PAD_DELETE:
					Editable edit = eanInput.getText();
					if (edit.length() > 0) {
						CharSequence text = edit.subSequence(0,
						        edit.length() - 1);
						eanInput.setText(text);
					}
					break;
				case PAD_RESET:
					eanInput.setText("");
					break;
			}

		}

	}
}
